<template>
  <div style="background-color: #42b983">
    <div style="position: relative;float: left;left: 10%; margin-top: 6%" >
      <img src="../../public/images/酷鲨2.png" width="80%">
    </div>
    <div style="width: 28%;margin: 14% 0 0 60%;position: absolute;top: 0px" >
      <h3>酷鲨商城运营管理平台</h3>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>
<script>
  export default {
    data() {
      return {
        ruleForm: {
          username: '',
          password:''
        },
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 8, max: 15, message: '长度在 8 到 15 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url="http://localhost:8081/login"
            let forName=this.ruleForm;
            console.log(url,forName)
            this.axios.post(url,forName).then( (response) =>{
              let responseBody=response.data
              if (responseBody==1){
                this.$message.success("登录成功")
              }else if (responseBody==2){
                this.$message.error("登录失败，用户名不存在")
              }else {
                this.$message.error("登录失败，密码错误")
              }
            });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style>
  h3{
    padding: 0 0 10% 25%  ;
  }


</style>
